<template>
	<view class="vOntrialPage">
		<view class="vBar">V+安装包</view>
		<view class="vListContent">
			<view v-for="(item,index) in list" :key="index" class="vList">
				<view class="tag" v-if="item.isExchange==1">
					<u--image src="/static/vOnTrial/1.png" shape="square" mode="aspectFill" width="82rpx"
						height="82rpx">
					</u--image>
				</view>
				<view class="vImage">
					<u--image :src="baseUrl+'/'+item.images" shape="square" mode="aspectFill" width="280rpx"
						height="192rpx" radius="12rpx">
					</u--image>
				</view>
				<view class="vInfo">
					<view class="title">{{item.title}}</view>
					<view class="integral">兑换：{{item.integral}}积分</view>
					<view class="vBtn color1" @click="doClip(item,1)" v-if="isSendArr.indexOf(item.id)>-1">复制链接</view>
					<view v-else>
						<view class="vBtn color2" @click="doChange(item)" v-if="item.isExchange==2">积分兑换</view>
						<view class="vBtn" @click="doChange(item,1)" v-if="item.isExchange==1">发送邮箱</view>
					</view>
				</view>
			</view>
		</view>
		<view class="vBar">V+月度授权</view>
		<view class="vListContent">
			<view v-for="(item,index) in list2" :key="index" class="vList">
				<view class="tag" v-if="item.isExchange==1">
					<u--image src="/static/vOnTrial/1.png" shape="square" mode="aspectFill" width="82rpx"
						height="82rpx">
					</u--image>
				</view>
				<view class="vImage">
					<u--image :src="baseUrl+'/'+item.images" shape="square" mode="aspectFill" width="280rpx"
						height="192rpx" radius="12rpx">
					</u--image>
				</view>
				<view class="vInfo">
					<view class="title">{{item.title}}</view>
					<view class="integral">兑换：{{item.integral}}积分</view>
					<view class="vBtn color1" @click="doClip(item)" v-if="isSendArr.indexOf(item.id)>-1">复制链接</view>
					<view v-else>
						<view class="vBtn color2" @click="doChange(item)" v-if="item.isExchange==2">积分兑换</view>
						<view class="vBtn" @click="doChange(item)" v-if="item.isExchange==1">发送邮箱</view>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="showExchange" :round="10" mode="bottom" @close="doClose">
			<view class="exchangePanel">
				<view class="title">积分兑换</view>
				<view class="integral">{{exchangeInfo.integral}}
					<view class="unit">积分</view>
				</view>
				<view class="detail">
					<view class="userScore" v-if="exchangeInfo.canExchange">当前积分{{userScore}}</view>
					<view class="userScore" v-if="!exchangeInfo.canExchange">当前积分不足，请到任务中心获取积分</view>
					<view class="vBtn" @click="doExchange(exchangeInfo)">{{exchangeInfo.btnText}}</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: this.$api.baseUrl,
				showExchange: false,
				userScore: 0,
				userId: "",
				myEmail: "",
				isLoad: false,
				exchangeInfo: {
					integral: "0",
					canExchange: false,
					btnText: "做任务"
				},
				list: [],
				list2: [],
				isSendArr:[]
			}
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
						this.initData();
						uni.hideLoading()
					})
				}
			})
		},
		onShow() {
			this.refreshUser()
		},
		methods: {
			initData() {
				Promise.all([this.$request.get(this.$api.onTrialList, {
						pageNo: 1,
						pageSize: 1000,
						type: "1",
						userId: this.userId
					}),
					this.$request.get(this.$api.onTrialList, {
						pageNo: 1,
						pageSize: 1000,
						type: "2",
						userId: this.userId
					})
				]).then(res => {
					this.list = res[0].result.records
					this.list2 = res[1].result.records
					if (this.isLoad) {
						uni.showToast({
							icon: "success",
							title: "兑换成功"
						})
					}
					this.isLoad = true;
					uni.hideLoading()
				})

			},
			refreshUser() {
				if (this.isLoad) {
					this.$request.get(this.$api.getUserById, {
						id: this.userId
					}).then(result => {
						uni.setStorage({
							key: "userInfo",
							data: result.result
						})
						this.userScore = result.result.integral;
						this.myEmail = result.result.educationMailbox;
						uni.hideLoading()
					})
				}
			},
			doClose() {
				this.showExchange = false
			},
			doClip(item,type){
				if(type==1){
					uni.setClipboardData({
						data: item.link,
						success: function () {
							uni.showToast({
								title:"复制成功",
								icon:"success"
							})
						}
					});
				}else{
					uni.setClipboardData({
						data: this.baseUrl + '/sys/common/static/' + item.fileList,
						success: function () {
							uni.showToast({
								title:"复制成功",
								icon:"success"
							})
						}
					});
				}
				
			},
			doChange(item, type) {
				if (item.isExchange == 1) {
					if (this.myEmail) {
						this.isSendArr.push(item.id);
						if (type == 1) {
							this.$request.get(this.$api.sendEmail, {
								email: this.myEmail,
								title: item.title,
								content: "V+软件下载链接：" + item.link
							}).then(res => {
								uni.showToast({
									icon: "success",
									title: "发送成功"
								})
								uni.hideLoading()
							})
						} else {
							let fileArr = item.fileList.split("/")
							this.$request.get(this.$api.sendEmailFile, {
								email: this.myEmail,
								title: item.title,
								filename: fileArr[fileArr.length - 1],
								filepath: this.baseUrl + '/sys/common/static/' + item.fileList,
								content:"点击附件下载文件"
							}).then(res => {
								uni.showToast({
									icon: "success",
									title: "发送成功"
								})
								uni.hideLoading()
							})
						}
					} else {
						uni.showModal({
							title: '温馨提示',
							content: '您还没有绑定邮箱，请去个人中心绑定邮箱账号',
							confirmText: "前去绑定",
							cancelText: "放弃领取",
							success: (res) => {
								if (res.confirm) {
									uni.navigateTo({
										url: "/pages/myInfo/myInfo"
									})
								} else if (res.cancel) {
									uni.showToast({
										icon: "none",
										title: "已取消发送"
									})
								}

							}
						})
					}

				} else {
					this.showExchange = true;
					if (item.integral > this.userScore) {
						this.exchangeInfo.id = item.id;
						this.exchangeInfo.canExchange = false;
						this.exchangeInfo.btnText = "做任务"
						this.exchangeInfo.integral = item.integral
						this.exchangeInfo.type = item.type
					} else {
						this.exchangeInfo.id = item.id;
						this.exchangeInfo.canExchange = true;
						this.exchangeInfo.btnText = "立即兑换"
						this.exchangeInfo.integral = item.integral
						this.exchangeInfo.type = item.type
					}
				}
			},
			doExchange(item) {
				if (item.integral > this.userScore) {
					uni.navigateTo({
						url: "/pages/taskCenter/taskCenter"
					})
				} else {
					this.$request.post(this.$api.doExchange, {
						integral: item.integral,
						otherId: item.id,
						userId: this.userId,
						exchangeType: item.type == '1' ? '1' : '4'
					}).then(res => {
						this.showExchange = false;
						this.initData();
						this.refreshUser();
						uni.hideLoading()
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.vOntrialPage {
		.vBar {
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;
			font-size: 36rpx;
			padding: 10rpx 32rpx 30rpx 32rpx;
			line-height: 36rpx;
		}

		.vListContent {
			display: flex;
			flex-direction: column;
			padding: 0 20rpx;

			.vList {
				display: flex;
				flex-wrap: nowrap;
				margin-bottom: 20rpx;
				padding: 32rpx 20rpx;
				background-color: #fff;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, 0.5);
				border-radius: 20rpx;
				position: relative;

				.tag {
					position: absolute;
					left: 0;
					top: 0;
					z-index: 1;
				}

				.vImage {
					padding-right: 20rpx;
				}

				.vInfo {
					display: flex;
					justify-content: space-between;
					flex-direction: column;

					.title {
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						font-size: 28rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 3;
						-webkit-box-orient: vertical;
					}
					.color1 {
						background: linear-gradient(180deg, #F3D748 0%, #EF7B17 100%)!important;
					}
					.color2 {
						background: linear-gradient(180deg, #F79037 0%, #F24929 100%)!important;
					}
					.integral {
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #EC6C2F;
						font-size: 26rpx;
					}

					.vBtn {
						background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
						width: 136rpx;
						height: 50rpx;
						border-radius: 34rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						font-size: 24rpx;
						line-height: 50rpx;
						text-align: center;
					}
				}
			}
		}

		.exchangePanel {
			display: flex;
			text-align: center;
			flex-direction: column;

			.title {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #666666;
				font-size: 28rpx;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;
			}

			.integral {
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				font-size: 46rpx;
				color: #EC6C2FFF;
				display: flex;
				flex-wrap: nowrap;
				justify-content: center;
				border-bottom: 1rpx solid #eee;
				height: 100rpx;
				line-height: 100rpx;

				.unit {
					font-size: 24rpx;
					position: relative;
					top: 6rpx;
					left: 2rpx;
					color: #333333FF;
				}
			}

			.detail {
				line-height: 100rpx;
				display: flex;
				align-items: center;
				flex-direction: column;
				padding-bottom: 40rpx;

				.userScore {
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 30rpx;
				}

				.vBtn {
					background: linear-gradient(180deg, #87C5FC 0%, #247FFF 100%);
					border-radius: 48rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					font-size: 34rpx;
					height: 96rpx;
					width: 680rpx;
				}
			}
		}
	}
</style>
